﻿using EasyAdmin.Application.Contracts.Systems.Roles;
using EasyAdmin.Application.Contracts.Systems.Users;
using EasyAdmin.Application.Contracts.Systems.Users.Dtos;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace EasyAdmin.Web.Host.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class UsersController : ControllerBase
    {
        private readonly IUserAppService _userAppService;
        public UsersController(IUserAppService userAppService)
        {
            _userAppService = userAppService;
        }

        /// <summary>
        /// 用户分页
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<dynamic> Page([FromQuery] UserPageCondition condition) {
            return await _userAppService.Page(condition);
        }

        /// <summary>
        /// 获得用户基本信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet("{userId}")]
        public async Task<dynamic> Get([FromRoute] long userId)
        {
            return await _userAppService.GetUserByUserId(userId);
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task Post([FromBody] CreateUserInput input) {
            await _userAppService.CreateUser(input);        
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task Put([FromBody] UpdateUserInput input)
        {
            await _userAppService.UpdateUser(input);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpDelete("{userId}")]
        public async Task Delete([FromRoute]long userId) {
            await _userAppService.DeleteUserByUserId(userId);
        }
    }
}
